#!/usr/bin/python
# -*- coding: UTF-8 -*-
# Filename : UpdateMouser.py
#Author:Bill Zhang
#Email:zchv@msn.com
#Mouser collection thread

#import httplib2
import httplib2
#import the BeautifulSoup which parse the html
from bs4 import BeautifulSoup
from bs4.diagnose import diagnose
#import python's reguler expression lib
import re
#import for flatten the list
import collections

def getData(URL):
	#URL='http://www.digikey.cn/product-detail/zh/BFC280800006/BC2767-ND/3028995'
	#URL=URL.replace('mouser.cn','mouser.com')
	h=httplib2.Http()
	#print(URL)
	headers={'Connection': 'keep-alive','Content-type': 'application/x-www-form-urlencoded','User-Agent':'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6','Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8','Accept-Language':'q=0.8,en-US;q=0.6,en;q=0.4'}
	#HtmlCode save the source code of the webpages
	resp,content=h.request(URL,'GET',headers=headers)
	if content!=0:
	    print("having got the html source")
	if resp.status == 200 :
		content=content.decode('utf-8')
		
		#print(content)
		##parse the html by using the BeautifulSoup

		#re extract content Mouser pattern
		Mouser=BeautifulSoup(content,["lxml", "xml"])

		#get product's quantity,if the number is not exsit,then the quantity will be set as 0;
		quantity=Mouser.find("table", { "id" : "ctl00_ContentMain_availability_tbl1" })
		if quantity is not None:
			quantity=quantity.text
			ProductQuantity=re.search(r"(?x)(\d*,?\d+)",quantity)
			if ProductQuantity is not None:
				ProductQuantity=int(ProductQuantity.group(0).replace(',',''))
			else:
				ProductQuantity=0
		else:
			ProductQuantity=0

		#Next line is for test the ProductQuantity
		print(ProductQuantity)


		##get product's price 
		##extract the number from the str

		price=Mouser.find("table", { "class" : "PriceBreaks" })
		if price is not None:
			price=price.text
			#print(price)		
			price=price.replace(',','')
			ProductPrice=re.findall(ur"(?x)(\d+):\W*¥(\d+\.?\d*)",price)
			if ProductPrice is None:
				ProductPrice=[(0,0)]
		else:
			ProductPrice=[(0,0)]
		ProductPrice=flatten(ProductPrice)
		ProductPrice.insert(0,ProductQuantity)
		ProductPrice = map(float, ProductPrice)
		return ProductPrice
	else:
		return 'retry'

def flatten(x):
	res=[]
	i=0
	while i<len(x):
		a=x[i][0]
		b=x[i][1]
		res.append(a)
		res.append(b)
		i=i+1
	return res


